package server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;


public class FileClassLoader extends ClassLoader {
    private static int byteLength;
    private String url;

    FileClassLoader(String url) {
        this.url = url;
    }

    public Class myLoadClass(String name) throws IOException {
        byte[] date = getBytesFromFile(name);
        return defineClass(name, date, 0, byteLength);
    }

    public byte[] getBytesFromFile(String name) throws IOException {
        FileInputStream fis = new FileInputStream(new File(
                url + name.replaceAll("\\.", "/") + ".class"));
        byte[] data = new byte[2048];
        int ch;
        while ((ch = fis.read(data)) != -1) {
            byteLength = ch;
        }
        return data;
    }
}
